package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.wineshop.WineShopBuilding;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/7/23 10:24
 */
public class WineShopConfigDaoImpl extends TempDao {
    public void parseWineShopBuildingMap(Map<Integer, Map<Integer, WineShopBuilding>> regionIdBuildingMap, Map<Integer, WineShopBuilding> wineShopBuildingMap) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_wineshop";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                WineShopBuilding data = new WineShopBuilding();
                data.setId(rs.getInt("id"));
                data.setBigType(rs.getInt("type"));
                data.setRegion(rs.getInt("region"));
                data.setSkillId(rs.getString("skillId"));
                data.setUpgradeItem(rs.getString("upgradeItem"));
                data.setUpgradeType(rs.getString("upgradeType"));
                data.setUnlock(rs.getLong("unlock"));
                data.setGuest(rs.getInt("guest"));
                data.setPreBuildingId(rs.getInt("preBuildingId"));
                String wellKnown = rs.getString("wellKnown");
                String[] split = wellKnown.split("\\|");
                data.setUnlockWellKnown(Integer.parseInt(split[0]));
                data.setUpgradeWellKnown(Integer.parseInt(split[1]));
                String money = rs.getString("money");
                String[] moneySplit = money.split("\\|");
                data.setBaseMoney(Integer.parseInt(moneySplit[0]));
                data.setUpgradeMoney(Integer.parseInt(moneySplit[1]));
                data.setSort(rs.getInt("sort"));
                data.setUpgradePar(rs.getString("upgradePar"));

                if (!regionIdBuildingMap.containsKey(data.getRegion())) {
                    regionIdBuildingMap.put(data.getRegion(), new ConcurrentHashMap<>());
                }
                regionIdBuildingMap.get(data.getRegion()).put(data.getId(), data);

                wineShopBuildingMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }
}
